/**
	Analysis
*/
package org.youroute.server.utils;


/**
Copyright (c) 2005-2006, Pavel Neytchev
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
Neither the name of the Pavel Neytchev nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
  
  
  
	Class analyses the password phrase

	Created on 26/01/2006

	Maximal password strength is reached when
	the sum of the (at the moment) five (5) strength criteria is 100 (in %),
	this means every check can return max 20 (in %)
	The percental share for one criteria is calculated with the formula:
	<p>
	<code>(lCurrentCriteriaCount * 100) / LENGTH(password_phrase)</code>
	<p>
	This means, if we want to calculate the percent of capitals in the password phrase, we do:
	<p>
	"passWORD" => 4 small letters, 4 capitals letters, length = 8
	so		   => (4 * 100) / 8 = 50%
	<p>
	The criteria are:
	<p>
	1.&nbsp; number of small letters contained in the phrase
	2.&nbsp; number of capital letters contained in the phrase
	3.&nbsp; number of digits
	4.&nbsp; what min|max length should has the phrase
	5.&nbsp; number of special symbols contained in the phrase (for example: %,$,!,and so on)
	<p>
	It is meaningful, when some of the criteria have variable accuracy
	<p>
	TODO :: 26.01.2006 :: take accuracy from settings

	@author Pavel Neytchev
*/
public class PwdStrAnalysis
{
	// counters
	protected int csymbols;
	protected int ccharacters;
	protected int ccaps;
	protected int cdigits;
	// return strength
	protected int symbols;
	protected int characters;
	protected int caps;
	protected int digits;
	protected int lengthStr;

	protected int strength;

	protected String pwd;

	// number of criteria. in case some more criteria are added later, then max for one criteria will be 100 / CRITERIA
	protected final int CRITERIA = 5;

	public PwdStrAnalysis()
	{
		 csymbols    = 0;
		 ccharacters = 0;
		 ccaps       = 0;
		 cdigits     = 0;

		 symbols	 = 0;
		 characters  = 0;
		 caps        = 0;
		 digits      = 0;
		 lengthStr   = 0;

		 strength    = 0;

		 pwd         = "";
	}

	/**
		Retrieves the calculated strength regarding symbols 
		@return symbols count
	*/
	public int getSymbolsStrength()    { return symbols;    }
	/**
		Retrieves the calculated strength regarding small characters 
		@return characters count
	*/
	public int getCharactersStrength() { return characters; }
	/**
		Retrieves the calculated strength regarding digits 
		@return digits count
	*/
	public int getDigitsStrength()     { return digits;     }
	/**
		Retrieves the calculated strength regarding capital letters
		@return capital letters count
	*/
	public int getCapsStrength()       { return caps;		}
	/**
		Retrieves the calculated strength regarding length
		@return phrase length
	*/
	public int getLengthStrength()     { return lengthStr;  }

	/**
		Retrieves the calculated summarized phrase strength
		@return password strength in per cents
	*/
	public int getPasswordStrength()   { return strength;   }

	/**
		Gets the rate for every single criteria
		@return share rate in per cent
	*/	
	protected int getMaxCriteriaPerShare()
	{
		return (100 / CRITERIA);
	}

	/**
		Calculates the sum of the five checks as integer describing strength in per cent
		@param password text phrase representing the password
		@return total strength in per cent
	*/
	public int analyse(String password)
	{
		pwd		  = password;
		
		for(int i = 0; i < pwd.length(); i++)
		{
			if ( Character.isDigit(pwd.charAt(i)) )
			{
				cdigits++;
			}

			if ( Character.isLowerCase(pwd.charAt(i)) )
			{
				ccharacters++;
			}

			if ( Character.isUpperCase(pwd.charAt(i)) )
			{
				ccaps++;
			}
		}

		strength  = checkSymbols();
		strength += checkDigits();
		strength += checkCaps();
		strength += checkChars();
		strength += checkLength();

		return strength;
	}

	/**
		Reinitializes local variables. &nbsp; This allows current object to be used instantly again
	*/
	public void reinitialize()
	{
		 csymbols    = 0;
		 ccharacters = 0;
		 ccaps       = 0;
		 cdigits     = 0;

		 symbols	 = 0;
		 characters  = 0;
		 caps        = 0;
		 digits      = 0;
		 lengthStr   = 0;

		 strength    = 0;

		 pwd         = "";
	}

	/**
		Calculates the special symbols strength
		<p>
		Speacial symbols at the moment are: 
		@return special symbols strength in per cent
	*/
	protected int checkSymbols()
	{
		int average   = 0;
		int from      = 0;

		//char[] syms   = {'`', '~', '!', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '-', '=', '{', '}', '|', '[', ']', '\\', ':', '"', ';', '\'', '<', '>', '?', ',', '.', '/'};
		char[] syms   = {'~', '!', '#', '%', '^', '(', ')', '_', '+', '-', '=', '{', '}', '|', '[', ']', ';', '<', '>', ',', '.', '/'};

		for(int i = 0; i < pwd.length(); i++)
		{
			for(int j = 0; j < syms.length; j++)
			{
				if (pwd.indexOf(syms[j], from)!=-1)
				{
					from = pwd.indexOf(syms[j], from) + 1;
					csymbols++;
					break;
				}
			}
		}

		if (pwd.length() > 0)
		{
			average = ((csymbols * 100) / pwd.length());
		}		

		if (10 <= average)
		{
			symbols = getMaxCriteriaPerShare();
		}
		else
		{
			if (csymbols > 0)
			{
				symbols = getMaxCriteriaPerShare() - 10;
			}
		}
	
		return symbols;
	}

	/**
		Calculates the digits strength
		@return digits strength in per cent
	*/
	protected int checkDigits()
	{
		int average = 0;

		if (pwd.length() > 0)
		{
			average = ((cdigits * 100) / pwd.length());
		}

		if (20 <= average)
		{
			digits = getMaxCriteriaPerShare();
		}
		else
		{
			if (cdigits > 0)
			{
				digits = getMaxCriteriaPerShare() - 10;
			}
		}
			
		return digits;
	}

	/**
		Calculates the capital lethers strength
		@return capital letters in per cent
	*/
	protected int checkCaps()
	{
		int average = 0;

		if (pwd.length() > 0)
		{		
			average = ((ccaps * 100) / pwd.length());
		}

		if (20 <= average)
		{
			caps = getMaxCriteriaPerShare();
		}
		else
		{
			if (ccaps > 0)
			{
				caps = getMaxCriteriaPerShare() - 10;
			}
		}

		return caps;
	}

	/**
		Calculates the small letters strength
		@return small letters strength in per cent
	*/
	protected int checkChars()
	{
		int average = 0;

		if (pwd.length() > 0)
		{		
			average = ((ccharacters * 100) / pwd.length());
		}

		if (20 <= average)
		{
			characters = getMaxCriteriaPerShare();
		}
		else
		{
			if (ccharacters > 0)
			{
				characters = getMaxCriteriaPerShare() - 10;
			}
		}

		return characters;
	}

	/**
		Calculates the phrase length strength
		@return length strength in per cent
	*/
	protected int checkLength()
	{
		if ((pwd.length() >= 7) && (pwd.length() <= 25))
		{
			lengthStr = getMaxCriteriaPerShare();
		}		

		return lengthStr;
	}
}
